package cn.hl.hm.controller.manage;

import cn.hl.hm.controller.driver.DriverInfoController;
import cn.hl.hm.logic.manage.VehicleManageLogic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 管理系统：车辆管理
 * Created by tarzan on 2017/10/31.
 */
@Controller
@RequestMapping(value = "/manage/driver/vehicle")
public class VehicleManageController {
    private static final Logger logger = LoggerFactory.getLogger(DriverInfoController.class);
    @Autowired
    VehicleManageLogic vehicleManageLogic;

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public String vehicleList(@CookieValue(required = false, value = "manageToken") String manageToken,
                              @RequestParam(required = true) Integer driverId,
                              @RequestParam(required = false) String plateNumber,
                              @RequestParam(required = false) String checkState,
                              HttpServletRequest request, ModelMap modelMap) throws IOException {
        logger.info("车辆---列表---/manage/driver/vehicle/list");
        try {
            vehicleManageLogic.getVehicleList(driverId, plateNumber, checkState, modelMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "manage/vehicle/vehicle.list";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addVehicle(@CookieValue(required = false, value = "manageToken") String manageToken,
                             @RequestParam(required = true) Integer driverId,
                             @RequestParam(required = false) String plateNumber,
                             @RequestParam(required = false) Integer vehicleTpyeId,
                             @RequestParam(required = false) Integer vehicleLengthId,
                             @RequestParam(required = false) String transportLicense,
                             @RequestParam(required = false) String drivingLicensePic,
                             @RequestParam(required = false) String drivingLicenseGuaPic,
                             HttpServletRequest request, ModelMap modelMap) throws IOException {
        logger.info("车辆---添加---/manage/driver/vehicle/add");
        try {
            vehicleManageLogic.addVehicle(driverId, plateNumber, vehicleTpyeId, vehicleLengthId,
                    transportLicense, drivingLicensePic, drivingLicenseGuaPic, modelMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "manage/vehicle/vehicle.result";
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public String deleteVehicle(@CookieValue(required = false, value = "manageToken") String manageToken,
                                @RequestParam(required = true) Integer driverId,
                                @RequestParam(required = true) Integer vehicleId,
                                HttpServletRequest request, ModelMap modelMap) throws IOException {
        logger.info("车辆---删除---/manage/driver/vehicle/delete");
        try {
            vehicleManageLogic.deleteVehicle(driverId, vehicleId, modelMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "manage/vehicle/vehicle.result";
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String updateVehicle(@CookieValue(required = false, value = "manageToken") String manageToken,
                                @RequestParam(required = true) Integer driverId,
                                @RequestParam(required = false) String plateNumber,
                                @RequestParam(required = false) Integer vehicleTpyeId,
                                @RequestParam(required = false) Integer vehicleLengthId,
                                @RequestParam(required = false) String transportLicense,
                                @RequestParam(required = false) String drivingLicensePic,
                                @RequestParam(required = false) String drivingLicenseGuaPic,
                                @RequestParam(required = false) Integer status,
                                HttpServletRequest request, ModelMap modelMap) throws IOException {
        logger.info("车辆---修改---/manage/driver/vehicle/update");
        try {
            vehicleManageLogic.updateVehicle(manageToken, driverId, plateNumber, vehicleTpyeId,
                    vehicleLengthId,
                    transportLicense, drivingLicensePic, drivingLicenseGuaPic, status, null, null, modelMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "manage/vehicle/vehicle.result";
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public String vehicleDetail(@CookieValue(required = false, value = "manageToken") String manageToken,
                                @RequestParam(required = true) Integer driverId,
                                @RequestParam(required = true) Integer vehicleId,
                                HttpServletRequest request, ModelMap modelMap) throws IOException {
        logger.info("车辆---详情---/manage/driver/vehicle/detail");
        try {
            vehicleManageLogic.vehicleDetail(driverId, vehicleId, modelMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "manage/vehicle/vehicle.detail";
    }

    @RequestMapping(value = "/check", method = RequestMethod.POST)
    public String checkVehicle(@CookieValue(required = false, value = "manageToken") String manageToken,
                               @RequestParam(required = true) Integer driverId,
                               @RequestParam(required = true) String plateNumber,
                               @RequestParam(required = true) Integer checkState,
                               @RequestParam(required = false) String driverUnpassReason,
                               HttpServletRequest request, ModelMap modelMap) throws IOException {
        logger.info("车辆---审核---/manage/driver/vehicle/check");
        try {
            vehicleManageLogic.updateVehicle(manageToken, driverId, plateNumber, null, null,
                    null, null, null, checkState, null, driverUnpassReason, modelMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "manage/vehicle/vehicle.result";
    }
}
